package com.sk.weichat.ui.base;

import android.os.Bundle;

public abstract class BaseFragment extends EasyFragment{
    protected boolean isViewInitiated;// 初始化view
    protected boolean isVisibleToUser;//fragment是否可见
    protected boolean isDataInitiated;//加载数据

    protected abstract void initView();
    @Override
    protected void onActivityCreated(Bundle savedInstanceState, boolean createView) {
        initView();
        isViewInitiated = true;
        prepareFetchData();
    }

    //查看这个fragment的可见状态
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }
    //在这个方法中写网络请求
    public abstract void loadingData();

    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            loadingData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

}
